﻿//copyright © kaveh.shahbazian@gmail.com and http://www.codeplex.com/site/users/view/Cread 2010-2011 (see license.txt)
using System.Windows;
using System.Net;
using System.Text;
using System.IO;
using System.Web;
using System.Diagnostics;
using System.Windows.Media;

namespace Funtastic.Windows
{
    /// <summary>
    /// Interaktionslogik für Pastebin.xaml
    /// </summary>
    public partial class Pastebin : Window
    {
        private string _pasteText = "";

        public Pastebin()
        {
            InitializeComponent();
        }

        public void Initialize(string pasteText)
        {
            // Upload
            _pasteText = pasteText;
        }

        public string GetExpirationString(string txt)
        {
            switch (txt)
            {
                case "10 Minutes": return "10M";
                case "1 Hour": return "1H";
                case "1 Day": return "1D";
                case "1 Month": return "1M";
                default: return "N";
            }
        }

        public string PasteText(string name, string text, string expiration, string highlight)
        {
            if (name == "") { name = "<PastebinUploader>"; }
            if (expiration == "") { expiration = "1D"; }
            if (highlight == "") { highlight = "text"; }
            if (text == "") { return ""; }
            try
            {
                string res = "";
                res += "paste_code=" + HttpUtility.UrlEncode(text);
                res += "&paste_format=" + highlight;
                res += "&paste_name=" + name;
                res += "&paste_expire_data=" + expiration;

                byte[] data = (new ASCIIEncoding()).GetBytes(res);

                WebRequest request = WebRequest.Create("http://pastebin.com/api_public.php");
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = data.Length;

                Stream stream = request.GetRequestStream();
                stream.Write(data, 0, data.Length);
                stream.Close();

                WebResponse response = request.GetResponse();
                stream = response.GetResponseStream();

                StreamReader sr = new StreamReader(stream);
                res = sr.ReadToEnd();

                sr.Close();
                stream.Close();

                return res;
            }
            catch { }
            return "";
        }

        private void btn_Upload_Click(object sender, RoutedEventArgs e)
        {
            _output.Text = PasteText(_name.Text, _pasteText, GetExpirationString(_expirationDate.SelectedItem.ToString()), "fsharp");
        }

        private void _output_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Process.Start(_output.Text);
        }

        private void _output_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            _output.Foreground = new SolidColorBrush(Colors.Blue);
        }

        private void _output_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            _output.Foreground = new SolidColorBrush(Colors.Black);
        }
    }
}
